package com.example.xd.default_call

import android.Manifest
import android.app.Application
import android.content.Intent
import android.os.Build

/**
 * @auther XuDan on 2020/8/25.
 *
 */
class DefaultCallApp : Application() {
    private val permissions: Array<String> = arrayOf(
        Manifest.permission.CALL_PHONE,
        Manifest.permission.CAMERA,
        Manifest.permission.READ_PHONE_STATE,
        Manifest.permission.PROCESS_OUTGOING_CALLS,//小米手机需要的权限
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.RECORD_AUDIO
    )

    override fun onCreate() {
        super.onCreate()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (DefaultCallUtil.hasPermission(this, permissions)) {
                if (!DefaultCallUtil.isDefaultCall(this)) {
                    val intent = Intent(this, DefaultCallPermissionActivity::class.java)
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    startActivity(intent)
                } else {
                    if (!DefaultCallUtil.isFloating(this)) {
                        val intent = Intent(this, DefaultCallPermissionActivity::class.java)
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        startActivity(intent)
                    }
                }
            } else {
                val intent = Intent(this, DefaultCallPermissionActivity::class.java)
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                startActivity(intent)
            }
        } else {
            toast(this, "版本太低，不支持默认通话")
        }
    }
}